package com.xydpeng.tech.order.dal.das.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xydpeng.tech.order.dal.dao.OrderMapper;
import com.xydpeng.tech.order.dal.das.OrderDas;
import com.xydpeng.tech.order.dal.entity.Order;
import com.xydpeng.tech.order.enums.OrderStatusEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 20031003
 * @description 针对表【order】的数据库操作Service实现
 * @createDate 2024-12-11 15:30:24
 */
@Service
public class OrderDasImpl extends ServiceImpl<OrderMapper, Order>
        implements OrderDas {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrderStatusEnum getOrderStatus(Long orderCode) {
        Order order = this.getOne(Wrappers.lambdaQuery(Order.class)
                .select(Order::getStatus)
                .eq(Order::getOrderCode, orderCode));
        return OrderStatusEnum.getByCode(order.getStatus());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateOrderStatusAtomic(Long orderCode, OrderStatusEnum fromStatus, OrderStatusEnum toStatus) {
        return this.update(Wrappers.lambdaUpdate(Order.class)
                .set(Order::getStatus, toStatus.getCode())
                .eq(Order::getOrderCode, orderCode)
                .eq(Order::getStatus, fromStatus.getCode()));
    }
}




